package UMC.Security;


import UMC.Data.Utility;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class AccessToken {

    /// <summary>
    /// 管理员角色
    /// </summary>
    public static final String AdminRole = "Administrators";
    /// <summary>
    /// 一般用户角色
    /// </summary>
    public static final String UserRole = "Users";
    /// <summary>
    /// 来宾角色
    /// </summary>
    public static final String GuestRole = "Guest";
    public String ContentType;

    Integer Timeout;

    public AccessToken(UUID tmpId) {
        this.Id = tmpId;
        this.ActiveTime = 0l;
    }

    public AccessToken() {
        this.ActiveTime = 0l;

    }

    public String Username;
    public UUID SId;
    public UUID Id;
    public String Roles;
    public Long ActiveTime;

    public Map<String, String> Data = new HashMap<>();
    public String _organizes;

    public boolean authorization(String key) {
        return true;
    }

    public boolean isAuthenticated() {

        if (!Utility.isEmpty(Username) && "?".equals(Username) == false) {
            return true;
        } else {

            return false;
        }

    }

    public void signOut() {


        this.login(Identity.create(this.Id, "?", ""), 1800);

    }


    public AccessToken put(String key, String value) {
        if (Utility.isEmpty(key) == false) {
            if (Utility.isEmpty(value)) {
                this.Data.remove(key);
            } else {
                this.Data.put(key, value);
            }
        }
        return this;
    }

    public AccessToken login(Identity Id, int timeout) {
        AccessToken auth = this;
//        auth.ContentType = contentType;
        auth.Timeout = timeout;
//        auth.Id = tmpId;
        auth.Username = Id.name();
        auth.SId = Id.id();
        auth.ActiveTime = new Date().getTime() / 1000;
        auth.Roles = null;

        switch (Utility.isNull(Id.name(), "")) {
            case "#":
            case "?":
                if (Utility.isEmpty(Id.alias()) == false) {
                    auth.Data.put("#", Id.alias());
                }
                break;
            default:

                auth.Data.put("#", Id.alias());
                if (Id instanceof Identity.Identification) {
                    String[] roels = ((Identity.Identification) Id)._roles;
                    if (roels != null) {
                        auth.Roles = String.join(",", roels);
                    }
                }
                break;
        }
        return auth;
    }

    public String get(String name) {
        return this.Data.get(name);
    }

    public AccessToken put(Map<String, String> NameValue) {
        this.Data.putAll(NameValue);
        return this;
    }

    public void commit() {
        this.ActiveTime = new Date().getTime() / 1000;
//        UMC.Security.Membership.Instance().Activation(this);
    }

    public Identity identity() {


        long time = Utility.isNull(this.ActiveTime, 0) + this.Timeout;

        String Alias = this.Data.get("#");//["#"] as String ??String.Empty;
        if (this.Timeout > 0 && time <= System.currentTimeMillis()) {
            return Identity.create(this.Id, "?", Alias);
        }
        if (Utility.isEmpty(this.Username)) {
            return Identity.create(this.Id, "?", Alias);
        }
        switch (this.Username) {
            case "?":
                return Identity.create(this.SId, "?", Alias);
            case "#":
                if (this.SId != null) {
                    return Identity.create(this.SId, "#", Alias);
                } else {
                    return Identity.create(this.Id, "?", Alias);
                }
            default:
                if (this.SId != null) {
                    if (Utility.isEmpty(this.Roles)) {
                        return Identity.create(this.SId, this.Username, Alias);

                    } else {
                        return Identity.create(this.SId, this.Username
                                , Alias, this.Roles.split(","));
                    }
                } else {
                    return Identity.create(this.Id, "?", Alias);
                }
        }
    }

    boolean IsContains(String value, String search) {
        // int start = 0;

        int index = value.indexOf(search, 0);
        while (index > -1) {
            if (index > 0) {
                if (value.charAt(index - 1) != ',') {
                    continue;
                }
            }
            int end = index + search.length();
            if (end < value.length()) {
                if (value.charAt(end) == ',') {
                    return true;
                }
            } else {
                return true;
            }
            index = value.indexOf(search, index);
        }
        return false;
    }

    public boolean isInRole(String role) {

        if (!Utility.isEmpty(this.Roles) && !Utility.isEmpty(role)) {
            if (IsContains(this.Roles, AdminRole)) {
                return true;
            } else {

                return IsContains(this.Roles, role);
            }

        }
        return false;
    }

    public boolean IsOrganizeMember(String organizeName) {
        if (!Utility.isEmpty(this._organizes) && !Utility.isEmpty(organizeName)) {


            return IsContains(this._organizes, organizeName);


        }

        return false;
    }

}